package com.gm.wholesale.module.main.presenter;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.custom.ui.customManger.CreateCustomFragment;
import com.gm.wholesale.module.custom.ui.customManger.CustomListFragment;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.constant.MenuData;
import com.gm.wholesale.module.aftersale.ui.AftersaleFragment;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.ui.goodManger.CreateGoodFragment;
import com.gm.wholesale.module.good.ui.goodManger.GoodMangerFragment;
import com.gm.wholesale.module.good.ui.mainGood.GoodMainFragment;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.order.bean.OrderDetailBean;
import com.gm.wholesale.module.order.ui.MainOrderFragment;
import com.gm.wholesale.module.order.ui.OrderMangerFragment;
import com.gm.wholesale.module.report.ui.ReportMangerFragment;
import com.gm.wholesale.module.setting.ui.SettingFragment;
import com.gm.wholesale.module.settlement.ui.main.AccountMainFragment;
import com.gm.wholesale.module.webview.mainWebView.MainWebFragment;
import com.gm.wholesale.view.HeaderView;

import java.util.ArrayList;
import java.util.List;

public class MainPresenter {
    public MainActivity mActivity;
    public FragmentManager mManager;
    public FragmentTransaction mTransaction;
    // todo 主界面右边商品部分
    public GoodMainFragment mGoodMainFragment;
    //todo 主界面左侧订单列表
    public MainOrderFragment mMainOrderFragment;
    //todo 订单管理
    public OrderMangerFragment orderMangerFragment;
    //todo 设置
    public SettingFragment settingFragment;
    //todo 对账
    public AccountMainFragment accountMainFragment;
    //todo 网页相关，客户，应收，报表，商品
    public MainWebFragment mainWebFragment;
    //todo 售后
    public AftersaleFragment aftersaleFragment;
    //todo 商品管理
    public GoodMangerFragment goodMangerFragment;
    //创建商品
    public CreateGoodFragment mCreateGoodFragment;

    private CustomListFragment mCustomListFragment;//客户列表

    private CreateCustomFragment createCustomFragment;

    private ReportMangerFragment mReportMangerFragment; //报表模块

    private List<Fragment> fragments=new ArrayList<>();

    public MainPresenter(MainActivity activity) {
        mManager = activity.getSupportFragmentManager();
        mActivity = activity;
        initMainView();
        //initFragment();
    }

    private void initMainView() {
        mGoodMainFragment = (GoodMainFragment) mManager.findFragmentById(R.id.goodFragment);
        mMainOrderFragment = (MainOrderFragment) mManager.findFragmentById(R.id.orderFragment);
    }

    public void updateMainUI(){

        mGoodMainFragment.updateKind();
    }


    private void initFragment() {
        if (orderMangerFragment == null) {
            orderMangerFragment = new OrderMangerFragment();
            mTransaction = mManager.beginTransaction();
            mTransaction.add(R.id.mainView, orderMangerFragment);
            mTransaction.commitAllowingStateLoss();
            hidUI(orderMangerFragment);
        }
        if (accountMainFragment == null) {
            accountMainFragment = new AccountMainFragment();
            mTransaction = mManager.beginTransaction();
            mTransaction.add(R.id.mainView, accountMainFragment);
            mTransaction.commitAllowingStateLoss();
            hidUI(accountMainFragment);
        }
    }

    private void hidMainUI() {
        if (mGoodMainFragment != null) {
            hidUI(mGoodMainFragment);
        }
        if (mMainOrderFragment != null) {
            hidUI(mMainOrderFragment);
        }
    }
    //todo 显示首页主界面
    public void showMainUI(){
        if (mGoodMainFragment != null) {
            showUI(mGoodMainFragment);
        }
        if (mMainOrderFragment != null) {
            showUI(mMainOrderFragment);
        }
        for (Fragment fragment:fragments){
            removeFragment(fragment);
        }
        fragments.clear();
        //关闭对账单详情界面 事件
        EvenBusUtil.closeSettleMentOrderDetail();

    }

    public void showFunctionUI(MenuData mCurrentMenuData, HeaderView headerView) {
        hidMainUI();
        //订单管理界面
        if (mCurrentMenuData.getType() == MenuData.ORDER_MANGER.getType()) {
            showOrderMangerUI();
        }
        //商品管理
       else if (mCurrentMenuData.getType() == MenuData.GOOD_MANGER.getType()){
            if (goodMangerFragment==null){
                goodMangerFragment=new GoodMangerFragment();
            }
            showReplaceUI(goodMangerFragment);
        }
        //设置界面
        else if (mCurrentMenuData.getType() == MenuData.SETTING.getType()) {
            if (settingFragment==null){
                settingFragment=new SettingFragment();
            }
            showReplaceUI(settingFragment);
        }
        //客户管理
        else if (mCurrentMenuData.getType()==MenuData.CUSTOM_MANGER.getType()){
            if (mCustomListFragment==null){
                mCustomListFragment=new CustomListFragment();
                mCustomListFragment.setIsGetData(true);
            }
            showReplaceUI(mCustomListFragment);
        }
        //应收账款
        else if (mCurrentMenuData.getType() == MenuData.RECEIV_MONEY.getType()) {
            if (accountMainFragment==null){
                accountMainFragment=new AccountMainFragment();
            }
            showReplaceUI(accountMainFragment);
        }
        else if (mCurrentMenuData.getType() == MenuData.SALES_REPORT.getType()){
            if (mReportMangerFragment==null){
                mReportMangerFragment=new ReportMangerFragment();
            }
            showReplaceUI(mReportMangerFragment);
        }
        //网页界面
        else {
            if (mainWebFragment!=null){
                mainWebFragment.onDestroy();
                mainWebFragment=null;
            }
            mainWebFragment=new MainWebFragment();
            mainWebFragment.setHeaderView(headerView);
            Bundle bundle = new Bundle();
            bundle.putString(MainWebFragment.key, mCurrentMenuData.getPath());
            mainWebFragment.setArguments(bundle);
            showReplaceUI(mainWebFragment);
        }
    }

    public void showOrderMangerUI(){
        if (orderMangerFragment==null){
            orderMangerFragment=new OrderMangerFragment();
        }
        showReplaceUI(orderMangerFragment);
    }
     //todo 显示售后界面
    public void showAftersaleFragment(OrderDetailBean data){
            aftersaleFragment=new AftersaleFragment();
          fragments.add(aftersaleFragment);
            Bundle bundle=new Bundle();
            bundle.putSerializable("data", data);
            aftersaleFragment.setArguments(bundle);
            mTransaction=mManager.beginTransaction();
            mTransaction.add(R.id.mainView,aftersaleFragment);
            mTransaction.commitAllowingStateLoss();

    }

    public void showCreateGoodUI(GoodListBean goodBeans){
        mCreateGoodFragment=new CreateGoodFragment();
        fragments.add(mCreateGoodFragment);
        if (goodBeans!=null){
            Bundle bundle=new Bundle();
            bundle.putSerializable("data",goodBeans);
            mCreateGoodFragment.setArguments(bundle);
        }
        mTransaction=mManager.beginTransaction();
        mTransaction.add(R.id.mainView,mCreateGoodFragment);
        mTransaction.commitAllowingStateLoss();
    }


    public void showCreateCustormUI(CustomBean customBean){
        createCustomFragment=new CreateCustomFragment();
        fragments.add(createCustomFragment);
        if (customBean!=null){
            Bundle bundle=new Bundle();
            bundle.putSerializable("data",customBean);
            createCustomFragment.setArguments(bundle);
        }
        mTransaction=mManager.beginTransaction();
        mTransaction.add(R.id.mainView,createCustomFragment);
        mTransaction.commitAllowingStateLoss();
    }

    //改方法会销毁前面加载的fragment
    private void showReplaceUI(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment==null)return;
        fragments.add(fragment);
        mTransaction.replace(R.id.mainView, fragment);
        mTransaction.commitAllowingStateLoss();
    }

    //该方法 不会 销毁前面加载的fragment，只是新增fragment到 mainview上面
    private void showAddUI(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment==null)return;
        mTransaction.replace(R.id.mainView, fragment);
        mTransaction.commitAllowingStateLoss();
    }

    private void hidUI(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment==null)return;
        mTransaction.hide(fragment);
        mTransaction.commitAllowingStateLoss();
    }

    private void showUI(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment==null)return;
        mTransaction.show(fragment);
        mTransaction.commitAllowingStateLoss();
    }

    private void removeFragment(Fragment fragment) {
        if (mManager == null) return;
        mTransaction = mManager.beginTransaction();
        if (mTransaction == null) return;
        if (fragment==null)return;
        mTransaction.remove(fragment);
        mTransaction.commitAllowingStateLoss();

    }


}
